public class Number14 {
    public String longestCommonPrefix(String[] strs) {
        int n = strs.length;
        if (n <= 1) return strs[0];
        String ans = strs[0];
        for (int i = 1; i < n; i++) {
            int len = check(ans,strs[i]);
            if (len == 0) return "";
            ans = ans.substring(0,len);
        }
        return ans;
    }

    public int check(String s1,String s2) {
        int len = Math.min(s1.length(),s2.length());
        for (int i = 0; i < len; i++) {
            if (s1.charAt(i) != s2.charAt(i)) return i;
        }
        return len;
    }
}
